package com.cjjd.c.feature.home.bill;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.cjjd.c.R;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.ReceiptBean;
import com.cjjd.c.feature.home.chekin.pay.PayModeAdapter;
import com.cjjd.c.utils.CommonUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ----------------------------------
 * Created by xyd on 2017/9/6.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class BillAdvanceFragment extends ActionBarFragment {
    @Bind(R.id.list_bill_history)
    ListView listBillHistory;

    private PayModeAdapter payModeAdapter;
    private int currentNum = -1;

    @Override
    public Object getTitleString() {
        return "发票办理";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_bill_advance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    protected void initView() {
        payModeAdapter = new PayModeAdapter(mContext);
        payModeAdapter.setFrom(payModeAdapter.TYPE_FROM_BILL);
        final ArrayList<ReceiptBean> objects = new ArrayList<>();
        objects.add(new ReceiptBean());
        objects.add(new ReceiptBean());
        objects.add(new ReceiptBean());
        payModeAdapter.setData(objects);
        listBillHistory.setAdapter(payModeAdapter);
        CommonUtil.setListViewHeightBasedOnChildren(listBillHistory);


        listBillHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (ReceiptBean itemBean : objects) {
                    itemBean.isSelected = false;
                }
                if (currentNum == -1) { //选中
                    objects.get(position).isSelected = true;
                    currentNum = position;
                } else if (currentNum == position) { //同一个item选中变未选中
                    for (ReceiptBean itemBean : objects) {
                        objects.get(position).isSelected = false;
                    }
                    currentNum = -1;
                } else if (currentNum != position) { //不是同一个item选中当前的，去除上一个选中的
                    for (ReceiptBean itemBean : objects) {
                        objects.get(position).isSelected = false;
                    }
                    objects.get(position).isSelected = true;
                    currentNum = position;
                }
                payModeAdapter.notifyDataSetChanged();//刷新adapter
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
